package com.mall.market.controller;

import com.mall.commons.utils.CommonResult;
import com.mall.market.service.SmsHomeRecommendProductService;
import com.mall.model.domain.SmsHomeRecommendProduct;
import com.mall.model.dto.SmsHomeRecommendProductDto;
import com.mall.model.vo.CommonPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/home/recommendProduct")
public class SmsHomeRecommendProductController {
    @Autowired
    private SmsHomeRecommendProductService smsHomeRecommendProductService;

    /**
     * 分页查询推荐
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(SmsHomeRecommendProductDto dto) {
        log.info("dto数据{}", dto);
        CommonPage commonPage = smsHomeRecommendProductService.list(dto);
        return CommonResult.success(commonPage);
    }

    //添加首页推荐
    @PostMapping("/create")
    public CommonResult create(@RequestBody List<SmsHomeRecommendProduct> smsHomeRecommendProductList) {
//        List list = JSON.parseArray(array.toJSONString());JSONArray array
        log.info("homeRecommendProductList：{}", smsHomeRecommendProductList);
        smsHomeRecommendProductService.create(smsHomeRecommendProductList);
        return CommonResult.success(1);
    }

    //批量删除推荐
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List ids) {
        smsHomeRecommendProductService.delete(ids);
        return CommonResult.success(1);
    }

    //批量修改推荐状态
    @PostMapping("/update/recommendStatus")
    public CommonResult updateStatus(@RequestParam List<Integer> ids, Integer recommendStatus) {
        smsHomeRecommendProductService.updateStatus(ids, recommendStatus);
        return CommonResult.success("修改成功");
    }

    //修改排序
    @PostMapping("update/sort/{id}")
    public CommonResult updateSort(@PathVariable("id") Integer id, Integer sort) {
        smsHomeRecommendProductService.updateSort(id, sort);
        return CommonResult.success("修改成功");
    }
}
